#
# Package.
#

package iSOCK::Model::Component;

#
# Dependencies.
#

use SOCK::SVN::Component;

#
# Parent.
#

use Moose;
use namespace::autoclean;

BEGIN { extends 'iSOCK::Model::Root' }

#
# Documentation.
#

=head1 NAME

iSOCK::Model::Component - Catalyst Model

=head1 DESCRIPTION

Catalyst Model.

=head1 AUTHOR

Xavier Caron

=head1 LICENSE

This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

#
# Subs.
#

sub lint {
  my ( $self, $option ) = @_;
  
  my $c       = $option->{c};
  my $command = "sock_lint_datasheet " . ( $option->{component_name} ? "--component $option->{component_name}" : "--datasheet $option->{datasheet_file}" ) . " --indent 2 --file_indent 0";
  
  if ( $self->spawn ( $c, $command, $option->{log} ) ) {
    return 1;
  }
  
  return;
}

sub atp2svn {
  my ( $self, $option ) = @_;
  
  my $c = $option->{c};
  
  if ( my $user_r = $c->model ( 'App' )->user_r ( $c ) ) {
    if ( $user_r->assert_user_role ( 'can_commit_datasheet' ) ) {
      my $parameters = $c->request->parameters;
      my $command    = "sock_atp_to_subversion --number $parameters->{atp_number}" . ( $parameters->{tag} ? " --tag" : "" ) . " --yes --user " . $user_r->id . " --svn_user " . $c->config->{subversion_user} . ( $parameters->{dummy} ? " --dummy" : "" ) . " --indent 2 --file_indent 0";

      if ( $self->spawn ( $c, $command, $option->{log} ) ) {
        return 1;
      }
    }
  }
  
  return;
}

sub session_keys {
  qw( component_names do_lint do_atp2svn );
}

sub repository_revision {
  my ( $self, $option ) = @_;
  
  if ( my $svn = SOCK::SVN::Component->new ({ svn_pipeless => 1, svn_tmp => $option->{c}->session->{session_path} }) ) {
    return $svn->revision;
  }
  
  return;
}

#
# True.
#

1;
